﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.OleDb;

namespace DataAccess
{
    public class DataSourceTransactionManager //: TransactionManager
    {
        private static OleDbConnection mycon=null;
        private static OleDbTransaction tran=null;
        private static string connectionstring = ConfigurationManager.ConnectionStrings["OleDbConnectionString"].ToString();

        #region TransactionManager 成员

        public static void BeginTransaction()
        {
            if (mycon != null)
            {
                tran = mycon.BeginTransaction();
            }
        }

        public static void CommitTransaction()
        {
            if (tran != null)
            {
                tran.Commit();
            }
        }

        public static void RollBackTransaction()
        {
            if (tran != null)
            {
                tran.Rollback();
            }
        }

        public static void Open()
        {
            if (mycon == null)
            {
                mycon = new OleDbConnection(connectionstring);
            }
            if (mycon.State == ConnectionState.Closed)
            {
                mycon.Open();
            }
        }

        public static void Close()
        {
            if (mycon != null)
            {
                if (mycon.State == ConnectionState.Open)
                {
                    mycon.Close();
                }
            }
        }

        public static int ExecuteNonQuery(string sql)
        {
            if (mycon == null || ConnectionState.Closed == mycon.State || tran == null)
                return 0;
            OleDbCommand mycmd = new OleDbCommand(sql, mycon);
            mycmd.Transaction = tran;
            return mycmd.ExecuteNonQuery();
        }

        public static int ExecuteNonQuery(string sql, OleDbParameter[] param)
        {
            if (mycon == null || ConnectionState.Closed == mycon.State || tran == null)
                return 0;
            OleDbCommand mycmd = new OleDbCommand(sql, mycon);
            mycmd.Transaction = tran;
            mycmd.Parameters.AddRange(param);
            return mycmd.ExecuteNonQuery();
        }

        public static OleDbDataReader ExecuteReader(string sql)
        {
            if (mycon == null || ConnectionState.Closed == mycon.State)
                return null;
            OleDbCommand mycmd = new OleDbCommand(sql, mycon);
            return mycmd.ExecuteReader();
        }

        public static OleDbDataReader ExecuteReader(string sql, OleDbParameter[] param)
        {
            if (mycon == null || ConnectionState.Closed == mycon.State)
                return null;
            OleDbCommand mycmd = new OleDbCommand(sql, mycon);
            mycmd.Parameters.AddRange(param);
            return mycmd.ExecuteReader();
        }

        public static DataSet ExecuteDataSet(string sql)
        {
            if (mycon == null || ConnectionState.Closed == mycon.State)
                return null;
            OleDbDataAdapter myadapter = new OleDbDataAdapter(sql, mycon);
            DataSet ds = new DataSet();
            myadapter.Fill(ds);
            return ds;
        }

        public static DataSet ExecuteDataSet(string sql, OleDbParameter[] param)
        {
            if (mycon == null || ConnectionState.Closed == mycon.State)
                return null;
            OleDbDataAdapter myadapter = new OleDbDataAdapter(sql, mycon);
            myadapter.SelectCommand.Parameters.AddRange(param);
            DataSet ds = new DataSet();
            myadapter.Fill(ds);
            return ds;
        }

        #endregion
    }
}
